﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using AKMII.DMRA.Common;
using AKMII.DMRA.Business.Management;
using AKMII.DMRA.Business.Rule;

namespace AKMII.DMRA.Web
{
    public partial class ViewOrderHistory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataList(null, null);
            }
        }

        #region private Methods
        private void BindDataList(string year, string month)
        {
             User user = Session["CurrentUser"] as User;
            if (year == null)
            {
                year = CurrentPeriod.Year.ToString();
            }
            if (month == null)
            {
                month = CurrentPeriod.Month.ToString();
            }
            OrderManager om = new OrderManager();
            List<LeadBlock> zips = om.GetOrderHistory(User.Identity.Name,
                   CurrentPeriod.Year ,CurrentPeriod.Month );
            Session["orderHistory"] = zips;
            DataList1.DataSource = zips;
            DataList1.DataBind();
        }
        #endregion

        #region protected methods
        /// <summary>
        /// show a fork for zip who have ordered in this month
        /// </summary>
        /// <param name="ZeroOrOne"></param>
        /// <returns></returns>
        protected string ShowFork(bool zeroOrOne)
        {
            if (zeroOrOne)
            {
                return "×";
            }
            return string.Empty;
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            List<LeadBlock> zips = (List<LeadBlock>)Session["orderHistory"];
            DataList1.DataSource = zips;
            DataList1.PageIndex = e.NewPageIndex;
            this.DataList1.DataBind();
        }
        #endregion

        #region public methods
        public string GetActiveLeadCount()
        {
            string leadCount = string.Empty;

            User user = Session["CurrentUser"] as User;

            if (user != null)
            {
                leadCount = (user.AgentNumber * SystemConfiguration.CommonSetting.LeadPerAgent * 4).ToString();
            }
            else
            {
                FormsAuthentication.SignOut();
                Response.Redirect(Constants.LoginUrl);
            }
            return leadCount;
        }
        public string GetLatestMonthByCurrentMonth(int i)
        {
            DateTime current = new DateTime(CurrentPeriod.Year, CurrentPeriod.Month, 1);

            return Utility.GetMonthName(current.AddMonths(i).Month).Substring(0, 3);
        }
        #endregion

    }
}
